//
//  ZZCarDebtRankVC.m
//  新项目
//
//  Created by liGordon on 17/3/13.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZCarDebtRankVC.h"
#import "ZZCarDebtRankCell.h"
#import "ZZCarDebtDetailVC.h"
#import "ZZCustomViewModel.h"
#import "ZZFinanceDataViewModel.h"
#import "ZZCarDebtSelectedView.h"
static NSString *identifier = @"CarDebtRankCell";
@interface ZZCarDebtRankVC ()<UITableViewDelegate,UITableViewDataSource,ZZCarDebtRankCellDelegate,ZZCarDebtSelectedViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *menuTable;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, copy) NSString * num;
@property (nonatomic, strong) CcNoDataView *noDataView;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) ZZCarDebtSelectedView *TanCView;
@property (nonatomic, copy) NSString *CarPlateStr;
@property (nonatomic, copy) NSString *CarOwnerStr;
@property (nonatomic,strong)NSString *nowStr;
@property (nonatomic,strong)NSString *beforeStr;
@end

@implementation ZZCarDebtRankVC
-(ZZCarDebtSelectedView *)TanCView
{
    if (_TanCView == nil) {
        _TanCView = [ZZCarDebtSelectedView ViewWithXib];
        _TanCView.delegate=self;
        
    }
    return _TanCView;
    
}
-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
    
}
-(CcNoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [[CcNoDataView alloc] init];
        _noDataView.backgroundColor=[UIColor whiteColor];
    }
    return _noDataView;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    self.menuTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.CarPlateStr = @"";
    self.CarOwnerStr = @"";
    self.title = @"单车欠款排行";
    　NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    
    
    NSDate *datenow = [NSDate date];
    
    
    
    NSString *nowtimeStr = [formatter stringFromDate:datenow];
    
    NSLog(@"nowtimeStr =  %@",nowtimeStr);
    NSDate * mydate = [NSDate date];
    
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    self.nowStr = [dateFormatter stringFromDate:[NSDate date]];
    
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comps = nil;
    
    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:mydate];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
    [adcomps setYear:0];
    
    [adcomps setMonth:-1];
    
    [adcomps setDay:0];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:mydate options:0];
    self.beforeStr = [dateFormatter stringFromDate:newdate];

    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_wxcb_01"] style:UIBarButtonItemStylePlain target:self action:@selector(clickForSeletecd)];
    self.navigationItem.rightBarButtonItem=item;
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.index=1;
        
        [self seletecdDataWIthIndex:[NSString stringWithFormat:@"%ld",self.index]];
        [self.menuTable.mj_footer  resetNoMoreData];
    }];
    self.menuTable.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.total>upNum*self.index) {
            self.index++;
            [self seletecdDataWIthIndex:[NSString stringWithFormat:@"%ld",self.index]];
        }
        else
        {
            [self.menuTable.mj_footer endRefreshingWithNoMoreData];
            
        }
        
    }];
    
    
    self.menuTable.mj_header=header;
    [self.menuTable.mj_header beginRefreshing];
    [self.view addSubview:self.TanCView];
    self.TanCView.frame=CGRectMake(0, -newheight-64-200, newwidth, newheight);
    
}

-(void)seletecdDataWIthIndex:(NSString *)index
{
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];

    [[ZZFinanceDataViewModel shareViewModel]getFinanceAnalysisWithStime:self.beforeStr andETime:self.nowStr andPlateNumber:self.CarPlateStr andCarOwner:self.CarOwnerStr andWXXM:@"" andZXR:@"" andGZ:@"" andPartsName:@"" andRepairsNo:@"" andMakeName:@"" andCreatorName:@"" anditype:@"" andCompanyID:loginDict[@"CompanyID"]  andPageIndex:index andPageSize:@(upNum) andType:@"1" andSuccess:^(NSDictionary *result) {
        NSLog(@"res=%@",result);
                [self.menuTable.mj_header endRefreshing];
                if (result) {
                    NSDictionary *resDicd=[WHJSON objectFormJsonString:result[@"resultJson"]];
                    NSLog(@"当前字典是%@",resDicd);
                    NSMutableArray *mutableArr=[NSMutableArray array];
                    for (NSDictionary *dic in resDicd[@"rows"]) {
                        [mutableArr addObject:dic];
                    }
                    if (self.index!=1) {
                        if (mutableArr.count==0) {
                            [self.menuTable.mj_footer endRefreshingWithNoMoreData];
                            return ;
                        }
                        [self.dataArr addObjectsFromArray: mutableArr];
                        [self.menuTable.mj_footer endRefreshing];
        
                    }
                    else
                    {
                        self.dataArr=mutableArr;
                        [self.menuTable.mj_header endRefreshing];
        
                    }
        
        
        
        
        
        
        
                    [self.noDataView dataIsEmpty:self.dataArr withView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:YES];
                    self.noDataView.label.text=@"暂无数据";
                    
                    self.total=[resDicd[@"total"] integerValue];
                    [self.menuTable reloadData];
                    
                }
                

    } andFailer:^{
                [self.menuTable.mj_header endRefreshing];
                [self.noDataView noNetwithView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:NO];
                //点击重新加载的block
                WEAKSELF
                self.noDataView.myBlock=^{
                    [weakSelf.menuTable.mj_header beginRefreshing];
                };
    }];
    
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZCarDebtDetailVC *vc = [[ZZCarDebtDetailVC alloc]init];
    vc.dic=self.dataArr[indexPath.row];
    vc.flag = 1;
    PushVC(vc)
    
}
-(void)clickDetail
{
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


//查询
-(void)clickForSeletecd
{
    [UIView animateWithDuration:0.25 animations:^{
        self.TanCView.frame=CGRectMake(0, 64, newwidth, newheight-64);
    }];
    
}


-(void)clickForSeletectedDataWithCarPlate:(NSString *)plate andOwnerName:(NSString *)name
{
    [self hiddenTheView];
    self.CarPlateStr = plate;
    self.CarOwnerStr = name;
    [self.menuTable.mj_header beginRefreshing];
}


//关闭
-(void)hiddenTheView
{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.TanCView.frame=CGRectMake(0, -newheight-64-200, newwidth, newheight);
    }];
    
}

- (void)setupTableView
{
    _menuTable.delegate = self;
    _menuTable.dataSource = self;
    _menuTable.rowHeight = 80;
    [_menuTable registerNib:[UINib nibWithNibName:@"ZZCarDebtRankCell" bundle:nil] forCellReuseIdentifier:identifier];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZCarDebtRankCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.dic=self.dataArr[indexPath.row];
    cell.delegate  = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



@end
